
import 'package:flutter/material.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/goods_list_model.dart';
import 'package:gold/model/list_model.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 商品首页状态控制
/// @author tangzhentao
/// @created at 21:39 2024/11/25

class GoodsHomeViewmodel extends Viewmodel {
  /// 刷新控制器
  final RefreshController controller = RefreshController();

  /// 数据列表
  final List<GoodsListModel> goodsList = [];

  /// 搜索控制器
  final TextEditingController editingController = TextEditingController();
  final FocusNode foucsNode = FocusNode();

  /// 页码
  int _page = 1;

  /// 销量排序，0=默认排序，1=从大到小，2=从小到大
  int _sales = 0;
  int get sales => _sales;

  /// 价格排序，0=默认排序，1=从大到小，2=从小到大
  int _price = 0;
  int get price => _price;

  /// 设置默认排序
  void setDefault() {
    _sales = 0;
    _price = 0;
    loadFirst();
  }

  /// 改变销量排序
  void changeSales() {
    if (_sales == 0) {
      _sales = 2;
    } else if (_sales == 1) {
      _sales = 2;
    } else {
      _sales = 1;
    }
    loadFirst();
  }

  /// 改变价格排序
  void changePrice() {
    if (_price == 0) {
      _price = 2;
    } else if (_price == 1) {
      _price = 2;
    } else {
      _price = 1;
    }
    loadFirst();
  }

  /// 加载数据
  Future loadFirst() async {
    setLoading();
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.productList,
        data: {'page': _page, 'filter': {'keywords': editingController.text}, 'sort': {'sales': _sales, 'price': _price}}
    );
    if (result.success) {
      goodsList.clear();
      goodsList.addAll(result.data?.list?? []);
      if (goodsList.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.canRefresh;
        controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 搜索
  Future search() async {
    final text = editingController.text;
    if (text.isNotEmpty) {
      loadFirst();
    } else {
      '请输入要搜索的商品名称'.toast();
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.productList,
        data: {'page': _page, 'filter': {'keywords': editingController.text}, 'sort': {'sales': _sales, 'price': _price}}
    );
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.clear();
        goodsList.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.productList,
        data: {'page': _page, 'filter': {'keywords': editingController.text}, 'sort': {'sales': _sales, 'price': _price}}
    );
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
